package com.yungu.swift.admin.route.report;

import com.yungu.swift.admin.base.CurrentUser;
import com.yungu.swift.admin.utils.DownloadUtils;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.model.admin.AdminParam;
import com.yungu.swift.base.model.admin.AdminReportParam;
import com.yungu.swift.utils.MapUtils;
import com.yungu.swift.utils.StringUtils;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.context.WebApplicationContext;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.List;
import java.util.Map;

import static com.yungu.swift.admin.config.NacosListener.SYS_CONFIG;

/**
 * @author : cuixiuyin
 * @date : 2019/9/2
 */
@Slf4j
@RestController
@Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS)
@RequestMapping("/admin/v1/report/**")
@Api(value = "【报表管理】", tags = "报表统计网关入口")
public class ReportAdmin {

    private static final String PREFFIX = "/admin/v1";

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping(method = RequestMethod.POST)
    public ResponseData report(@RequestBody AdminReportParam reportParam, HttpServletRequest request) {
        String reportUrl = SYS_CONFIG.getReportHost() + StringUtils.remove(request.getRequestURI(), PREFFIX);
        return restTemplate.postForObject(reportUrl, reportParam, ResponseData.class);
    }

    @RequestMapping(value = "/export", method = RequestMethod.POST)
    public void export(@RequestBody Map<String, Object> exportParam, @CurrentUser AdminParam adminParam,
                       HttpServletRequest request, HttpServletResponse response) throws IOException {
        Map<String, Object> userMap = MapUtils.convertObjToMap(adminParam);
        exportParam.putAll(userMap);
        String reportUrl = SYS_CONFIG.getReportHost() + StringUtils.remove(request.getRequestURI(), PREFFIX);
        ResponseEntity<Resource> postForEntity = restTemplate.postForEntity(reportUrl, exportParam, Resource.class);
        if (postForEntity.getStatusCode().equals(HttpStatus.OK)) {
            String fileName = "";
            List<String> list = postForEntity.getHeaders().get("fileName");
            if (CollectionUtils.isNotEmpty(list)) {
                fileName = list.get(0);
            }
            DownloadUtils.download(response, postForEntity.getBody().getInputStream(), URLDecoder.decode(fileName, "UTF-8"));
        }
    }

}
